package com.opmsys.controller;

import com.opmsys.pojo.dto.RecordDTO;
import com.opmsys.pojo.entity.Record;
import com.opmsys.result.PageResult;
import com.opmsys.result.Result;
import com.opmsys.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 名称RecordController
 * 描述
 *
 * @version 1.0
 * @author:冉启呈2021015445
 * @datatime:2024-06-19 16:56
 */
@RestController
@RequestMapping("/records")
public class RecordController {
    @Autowired
    private RecordService recordService;


    /**
     * 新增运转记录
     * @param record
     * @return
     */
    @PostMapping
    public Result addRecord(Record record) {
        List<Record> isRepeat=recordService.ifdate(record.recordDate);
        if(isRepeat.size()==0){
            recordService.addRecord(record);
            return Result.success();
        }
        else
            return Result.error("日期重复");
    }


    /**
     * 批量删除日记录
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deletePumpingBatch(@RequestParam(required = false) List<Integer> ids) {
        for (Integer id : ids)
            recordService.deleteRecord(id);
        return Result.success();
    }


    /**
     * 根据id更新日记录
     * @param record
     * @return
     */
    @PutMapping
    public Result updateRecord(Record record) {
        List<Record> isRepeat=recordService.ifdate(record.recordDate);
        if(isRepeat.size()==0){
            recordService.updateRecord(record);
            return Result.success();
        }
        else
            return Result.error("日期重复");
    }


    /**
     * 按条件分页查询记录
     * @param recordDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> searchRecord(RecordDTO recordDTO) {
        PageResult result=new PageResult();
        List<Record> records=recordService.getRecordPageWithConditions(recordDTO);
        List<Record> records1=recordService.getRecordListWithConditions(recordDTO);
        result.setTotal(records1.size());
        result.setRecords(records);
        return Result.success(result);
    }
}
